<template>
	<div class="app-container">
		<el-form class="queryForms" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="记录编号" prop="recordNumber">
				<el-input v-model="queryParams.recordNumber" placeholder="请输入记录编号" clearable maxlength="20" show-word-limit  @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="编号" prop="serialNumber">
				<el-input v-model="queryParams.serialNumber" placeholder="请输入编号" clearable maxlength="20" show-word-limit  @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<right-toolbar menuNames="synthesize53" @changeTableData="changeTableData" :showSearch.sync="showSearch" @queryTable="handleQuery" :columns.sync="columns"></right-toolbar>
		</el-row>

		<div :class="eleTabTop <= 85 ? 'eleTab' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table"  v-loading="loading" :data="propertyRegistrationRecordList" @selection-change="handleSelectionChange" row-key="id" lazy :load="loadListDes" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" width="55" >
           <template slot-scope="scope">
               <div>{{scope.row.sort}}</div>
           </template>
        </el-table-column>

				<el-table-column label="记录编号" prop="recordNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="编号" prop="serialNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="日期" min-width="200" :show-overflow-tooltip="true" prop="outsourcingDate" v-if="columns[2].visible" />
				<el-table-column prop="deviceTypeId" label="顾客名称" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[3].visible">
					<template slot-scope="scope">
						<el-select v-model="scope.row.deviceTypeId" @change="selectdeviceType(scope.row)" v-if="!scope.row.hasChildren">
							<el-option v-for="dict in dict.type.default_custom_name" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="生产批次" prop="productionBatch" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[4].visible" />
				<el-table-column label="物料名称" prop="partName" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="物料编码" prop="partFigure" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" />
				<el-table-column label="数量" align="center" prop="quantityquantity" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible" />
				<el-table-column label="发放人" align="center" prop="issuerName" min-width="150" :show-overflow-tooltip="true" v-if="columns[8].visible" />
				<el-table-column label="发放日期" align="center" prop="issuerDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[9].visible" />
				<el-table-column label="合格证接收人" align="center" prop="certificateRecipientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[10].visible" />
				<el-table-column label="接收日期" align="center" prop="certificateRecipientDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[11].visible" />
				<el-table-column label="零件接收人" align="center" prop="partRecipientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[12].visible" />
				<el-table-column label="接收日期" align="center" prop="partRecipientDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[13].visible" />
				<el-table-column label="状态" align="center" prop="outerApprovalStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[14].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.outerApprovalStatus == '0'">未提交</div>
						<div v-if="scope.row.outerApprovalStatus == '1'">审批中</div>
						<div v-if="scope.row.outerApprovalStatus == '2'">已通过</div>
						<div v-if="scope.row.outerApprovalStatus == '3'">未通过</div>
					</template>
				</el-table-column>
				<el-table-column label="设备状态" align="center" prop="registrationStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[15].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.registrationStatus == '0'">未提交</div>
						<div v-if="scope.row.registrationStatus == '1'">已提交</div>
						<div v-if="scope.row.registrationStatus == '2'">外委中</div>
						<div v-if="scope.row.registrationStatus == '3'">合格证已上传</div>
						<div v-if="scope.row.registrationStatus == '4'">待接收</div>
						<div v-if="scope.row.registrationStatus == '5'">已接收</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
					<template slot-scope="scope">
						<el-button v-if="!scope.row.hasChildren && scope.row.registrationStatus == '3'" size="mini" type="text" icon="el-icon-circle-check" @click="handleGrant(scope.row)" v-hasPermi="['erp:synthesize:grant']">发放</el-button>
						<el-button size="mini" type="text" icon="el-icon-s-order" @click="handleUpload(scope.row)" v-hasPermi="['erp:synthesize:upload']" v-if="!scope.row.hasChildren">上传</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:synthesize:delete']" v-if="scope.row.hasChildren">删除</el-button>
						<el-button size="mini" type="text" icon="el-icon-folder" @click="handleFile(scope.row)" v-hasPermi="['erp:synthesize:file']" v-if="!scope.row.hasChildren">附件</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination56"  v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改顾客财产登记(外委)记录对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="记录编号" prop="recordNumber">
					<el-input v-model="form.recordNumber" placeholder="请输入记录编号" />
				</el-form-item>
				<el-form-item label="编号" prop="serialNumber">
					<el-input v-model="form.serialNumber" placeholder="请输入编号" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入备注" />
				</el-form-item>
				<el-form-item label="删除标志" prop="delFlag">
					<el-input v-model="form.delFlag" placeholder="请输入删除标志" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="上传文件" :visible.sync="uploadOpen" width="900px" append-to-body v-dialogDrag>
			<el-form :model="uploadForm" label-width="140px" class="add-form1">
				<el-form-item label="上传文件">
					<FileUpload @input="getFiles" :value="uploadList"></FileUpload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitFormUpload">确 定</el-button>
				<el-button @click="uploadOpen = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { uploadCertificate, grant, listPropertyRegistrationRecord, getPropertyRegistrationRecord, delPropertyRegistrationRecord, addPropertyRegistrationRecord, updatePropertyRegistrationRecord } from "@/api/taskFilling/propertyRegistrationRecord"
import { listPropertyRegistrationOuter } from "@/api/taskFilling/propertyRegistrationOuter"
import { getcolumn } from "@/api/selectList"
export default {
	name: "synthesize",
	dicts: ["default_custom_name"],
	data() {
		return {
			eleTabTop: 0, //表格容器距浏览器顶部距离
			treetablekey: Math.random(),
			// 上传
			uploadOpen: false,
			uploadList: [],
			uploadForm: {},
			// 审批数据
			approveForm: {},
			openApprove: false,
			// 更新树刷新
			treeMap: new Map(),
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 顾客财产登记(外委)记录表格数据
			propertyRegistrationRecordList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				recordNumber: null,
				serialNumber: null,
				recordStatus: null,
				outerApprovalStatus: null
			},
			// 表单参数
			form: {},
			approveForm: {},
			// 表单校验
			rules: {
				delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `记录编号`, visible: true },
				{ key: 1, label: `编号`, visible: true },
				{ key: 2, label: `日期`, visible: true },
				{ key: 3, label: `顾客名称`, visible: true },
				{ key: 4, label: `生产批次`, visible: true },
				{ key: 5, label: `物料名称`, visible: true },
				{ key: 6, label: `物料编码`, visible: true },
				{ key: 7, label: `数量`, visible: true },
				{ key: 8, label: `发放人`, visible: true },
				{ key: 9, label: `发放日期`, visible: true },
				{ key: 10, label: `合格证接收人`, visible: true },
				{ key: 11, label: `接收日期`, visible: true },
				{ key: 12, label: `零件接收人`, visible: true },
				{ key: 13, label: `接收日期`, visible: true },
				{ key: 14, label: `状态`, visible: true },
				{ key: 15, label: `设备状态`, visible: true }
			]
		}
	},
	created() {
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination56" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		handleFile(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id.substring(1),
					typeId: 20
				}
			})
		},
		submitFormUpload() {
			if (this.uploadList.length == 0) {
				this.$modal.msgError("请先上传文件")
				return false
			}
			this.uploadForm.fileList = this.uploadList
			uploadCertificate(this.uploadForm).then(response => {
				this.$modal.msgSuccess("操作成功")
        this.$refs.table.clearSelection()
				this.uploadOpen = false
			})
		},
		// 上传
		getFiles(files) {
			this.uploadList = files
		},
		handleGrant(row) {
			console.log(row.id)
			this.$modal
				.confirm(`是否确认发放顾客财产-外委？`)
				.then(function () {
					return grant([row.id.substring(1)])
				})
				.then(() => {
					this.$modal.msgSuccess("发放成功")
          this.$refs.table.clearSelection()
					this.getList()
				})
				.catch(() => {})
		},
		handleUpload(row) {
			this.uploadList = []
			this.uploadOpen = true
			this.uploadForm.id = row.id.substring(1)
		},
		handleEdit(row) {},
		// 获取列表子菜单
		async loadListDes(tree, treeNode, resolve) {
			if (!this.treeMap.has(tree.id)) {
				this.treeMap.set(tree.id, {
					treeNode,
					resolve
				})
			}
			let ret = await listPropertyRegistrationOuter({ id: tree.id })
			ret.rows.forEach((item,index) => {
				// 父子id会冲突
				item.id = "c" + item.id
         item.sort = index+1
			})
			resolve(ret.rows)
		},
		/** 查询顾客财产登记(外委)记录列表 */
		getList() {
			this.loading = true
			listPropertyRegistrationRecord(this.queryParams).then(response => {
				this.propertyRegistrationRecordList = response.rows
				this.propertyRegistrationRecordList.forEach((item,index) => {
					item.hasChildren = true
           item.sort = index+1
          this.$refs.table.toggleRowExpansion(item, false);
				})
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				recordNumber: null,
				serialNumber: null,
				recordStatus: null,
				outerApprovalStatus: null,
				remark: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.treetablekey = Math.random()
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加顾客财产登记(外委)记录"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getPropertyRegistrationRecord(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改顾客财产登记(外委)记录"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != null) {
						updatePropertyRegistrationRecord(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addPropertyRegistrationRecord(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除顾客财产登记(外委)记录数据项？")
				.then(function () {
					return delPropertyRegistrationRecord(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
          this.$refs.table.clearSelection()
				})
				.catch(() => {})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/propertyRegistrationRecord/export",
				{
					...this.queryParams
				},
				`propertyRegistrationRecord_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
